package com.mine.site.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.mine.site.dao.CommodityDao;
import com.mine.site.model.Commodity;
import com.mine.site.model.ShoppingCart;





@SuppressWarnings("unused")
@WebServlet("/CommodityServlet")
public class CommodityServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("utf-8");
		
		
		HttpSession session = request.getSession();
		
		
		ServletContext ctx = request.getServletContext();

		PrintWriter out =  response.getWriter();
		
		String method = request.getParameter("m");
		if("del".equals(method)){
			this.del(request, response);
		}else if("forEdit".equals(method)){
			this.beforeEdit(request, response);
		}else if("update".equals(method)){
			this.update(request, response);
		}else if("save".equals(method)){
			this.save(request, response);
		}else if("byname".equals(method)){
			this.bynamme(request, response);
		}else if("all".equals(method)){
			this.all(request, response);
		}
		else if("successAll".equals(method)){
			this.successAll(request, response);
		}
		else{
			this.list(request, response);
		}
	}

	private void successAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		CommodityDao dao = new CommodityDao();
		List<ShoppingCart> Commo_successAll = dao.findAll_successAll();
		request.setAttribute("CommoList", Commo_successAll);
		request.getRequestDispatcher("successAll.jsp").forward(request, response);
		
	}

	private void all(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		CommodityDao dao = new CommodityDao();
		List<Commodity> Commo_all = dao.findAll_all();
		request.setAttribute("CommoList", Commo_all);
		request.getRequestDispatcher("newest.jsp").forward(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
	
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		CommodityDao dao = new CommodityDao();
		List<Commodity> Commo = dao.findAll1();
		request.setAttribute("CommoList", Commo);		
		request.getRequestDispatcher("commodity.jsp").forward(request, response);
	}
	
	private void bynamme(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String Name = request.getParameter("Name");

		CommodityDao dao = new CommodityDao();
		List<Commodity> commo = dao.findByName(Name);
		request.setAttribute("CommoList", commo);
		request.setAttribute("sou", Name);
		request.getRequestDispatcher("query.jsp").forward(request, response);
	}
	
	private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("utf-8");
		String Number = request.getParameter("Number");
		CommodityDao dao = new CommodityDao();
		dao.delete(Number);
		response.sendRedirect("CommodityServlet");
	}
	
	private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("utf-8");
		
		
		PageContext pc =  JspFactory.getDefaultFactory().getPageContext(this, request, response, null, false, JspWriter.DEFAULT_BUFFER, true);
		
		SmartUpload su  = new SmartUpload();

			su.initialize(pc);
		
				
				
				try {
					su.upload();
				} catch (SmartUploadException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				Commodity u = wrapUser(su.getRequest());
				
				//su.save("upload");
				String path = "";
				Files files = su.getFiles();
				if(files == null ) return;
				for(int i=0;i<files.getCount();i++){
					File file = files.getFile(i);
					path = file.getFileName().substring(0,file.getFileName().indexOf("."))+"."+file.getFileExt();
					if(i == 0){
						u.setUrl(path);
					}else if(i == 1){
						u.setImg_1(path);	
					}else if(i == 2){
						u.setImg_2(path);	
					}else if(i == 3){
						u.setImg_3(path);	
					}else if(i == 4){
						u.setImg_4(path);	
					}else{
						u.setMinute(path);	
					}
					
					try {
						file.saveAs("/img_shop/"+path);
					} catch (SmartUploadException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
		
		

		CommodityDao dao = new CommodityDao();
		dao.save(u);
		response.sendRedirect("CommodityServlet");
	}
	
	private void beforeEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String Number = request.getParameter("Number");
		CommodityDao dao = new CommodityDao();
		Commodity u = dao.findByNumber(Number);
		request.setAttribute("CommoList", u);
		request.getRequestDispatcher("editCom.jsp").forward(request, response);

	}
	
	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		Commodity u = wrapUser1(request);
		CommodityDao dao = new CommodityDao();
		dao.update(u);
		response.sendRedirect("CommodityServlet");
	}

	private Commodity wrapUser1(HttpServletRequest request) {

		
		Commodity u = new Commodity();
		u.setNumber(Integer.parseInt(request.getParameter("Number")));
		u.setName(request.getParameter("Name"));
		u.setTitle(request.getParameter("Title"));
		u.setPrice(request.getParameter("Price"));
		u.setUrl(request.getParameter("Url"));
		u.setImg_1(request.getParameter("Img_1"));
		u.setImg_2(request.getParameter("Img_2"));
		u.setImg_3(request.getParameter("Img_3"));
		u.setImg_4(request.getParameter("Img_4"));
		u.setMinute(request.getParameter("Minute"));
		u.setType(request.getParameter("Type"));
		return u;
	}
	private Commodity wrapUser(Request request) {
		
		Commodity u = new Commodity();
		u.setNumber(Integer.parseInt(request.getParameter("Number")));
		u.setName(request.getParameter("Name"));
		u.setTitle(request.getParameter("Title"));
		u.setPrice(request.getParameter("Price"));
		u.setUrl(request.getParameter("Url"));
		u.setImg_1(request.getParameter("Img_1"));
		u.setImg_2(request.getParameter("Img_2"));
		u.setImg_3(request.getParameter("Img_3"));
		u.setImg_4(request.getParameter("Img_4"));
		u.setMinute(request.getParameter("Minute"));
		u.setType(request.getParameter("Type"));
		return u;
	}
}
